<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\tool\Result;
use think\exception\ValidateException;
use app\common\model\Bank;
use app\common\model\User as UserModel;
use think\Response;

class Method extends Base
{
    public function index(): Response
    {
        $list = ['bank', 'wechat', 'alipay', 'usdt'];
        $result = [];
        foreach ($list as $item) {
            if (!empty($this->system['draw_' . $item . '_open'])) {
                $result[] = [
                    'title' => get_pay_way($item),
                    'type' => $item,
                    'icon' => "/static/admin/img/{$item}.png"
                ];
            }
        }

        return Result::success('success', $result);
    }

    public function info($type = 'bank'): Response
    {
        switch ($type) {
            case "bank":
                $result['title'] = get_pay_way('bank');
                $result['type'] = 'bank';
                $result['icon'] = '/static/admin/img/bank.png';
                $result['bank_list'] = Bank::where(['status' => 1])->order('sort desc')->column('title');
                $result['bank_name'] = $this->user->bank_name;
                $result['bank_open_name'] = $this->user->bank_open_name;
                $result['bank_number'] = $this->user->bank_number;
                $result['bank_address'] = $this->user->bank_address;
                break;
            case "wechat":
                $result['title'] = get_pay_way('wechat');
                $result['type'] = 'wechat';
                $result['icon'] = '/static/admin/img/wechat.png';
                $result['wechat_name'] = $this->user->wechat_name;
                $result['wechat_image'] = $this->user->wechat_image;
                break;
            case "alipay":
                $result['title'] = get_pay_way('alipay');
                $result['type'] = 'alipay';
                $result['icon'] = '/static/admin/img/alipay.png';
                $result['alipay_name'] = $this->user->alipay_name;
                $result['alipay_image'] = $this->user->alipay_image;
                break;
            case "usdt":
                $result['title'] = get_pay_way('usdt');
                $result['type'] = 'usdt';
                $result['icon'] = '/static/admin/img/usdt.png';
                $result['usdt_type'] = $this->user->usdt_type;
                $result['usdt_address'] = $this->user->usdt_address;
                $result['usdt_image'] = $this->user->usdt_image;
                break;
            default:
                return Result::fail('标识有误');
        }

        return Result::success('success', $result);
    }

    public function save(): Response
    {
        $param = $this->request->post();
        $type = $param['type'];
        try {
            $this->validate($param, 'Method.' . $type);
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }
        UserModel::update($param, ['id' => $this->user->id]);

        return Result::success('保存成功');
    }
}